package com.ysd.lis.service.impl;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkKnowledgeCatalog;
import com.ysd.lis.entity.WkKnowledgeContent;
import com.ysd.lis.mapper.WkKnowledgeCatalogMapper;
import com.ysd.lis.mapper.WkKnowledgeContentMapper;
import com.ysd.lis.request.KnowledgePageParam;
import com.ysd.lis.service.WkKnowledgeCatalogService;
import com.ysd.lis.util.ListToTreeUtil;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-03-04
 */
@Service
@Slf4j
public class WkKnowledgeCatalogServiceImpl extends ServiceImpl<WkKnowledgeCatalogMapper, WkKnowledgeCatalog> implements WkKnowledgeCatalogService {

    @Autowired
    WkKnowledgeCatalogMapper wkKnowledgeCatalogMapper;
    @Autowired
    WkKnowledgeContentMapper wkKnowledgeCatalogContentMapper;

    /**
     * 获取知识库Tree
     *
     * @param knowledgeId
     * @return
     */
    @Override
    public Result getKnowledgeCatalogTree(String knowledgeId) {
        LambdaQueryWrapper<WkKnowledgeCatalog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkKnowledgeCatalog::getKnowledgeId, knowledgeId)
                .eq(WkKnowledgeCatalog::getDelFlag, 0);
        List<WkKnowledgeCatalog> wkKnowledgeCatalogs = wkKnowledgeCatalogMapper.selectList(lambdaQueryWrapper);
        JSONArray objects = ListToTreeUtil.forObjectToTree(wkKnowledgeCatalogs, "0", "id", "pid", "children");
        return Result.succ(1, "查询成功", objects);
    }

    /**
     * 获取知识库Tree
     *
     * @param knowledgeId
     * @return
     */
    @Override
    public Result getKnowledgeCatalogAndContentTree(String knowledgeId) {
        List<WkKnowledgeCatalog> wkKnowledgeCatalogs = getKnowledgeCatalogAndContentData(knowledgeId);
        JSONArray objects = ListToTreeUtil.forObjectAndLevelToTree(wkKnowledgeCatalogs, "0", "id", "pid", "children", 1);
        return Result.succ(1, "查询成功", objects);
    }

    public List<WkKnowledgeCatalog> getKnowledgeCatalogAndContentData(String knowledgeId) {
        MPJLambdaWrapper<WkKnowledgeCatalog> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper
                .distinct()
                .selectAll(WkKnowledgeCatalog.class)
                .selectCollection(WkKnowledgeContent.class, WkKnowledgeCatalog::getWkKnowledgeContentList)
                .leftJoin(WkKnowledgeContent.class,on-> on.eq(WkKnowledgeContent::getCatalogId, WkKnowledgeCatalog::getId).ge(WkKnowledgeContent::getStatus,10))
                .eq(WkKnowledgeCatalog::getKnowledgeId, knowledgeId)
                .eq(WkKnowledgeCatalog::getDelFlag, 0);
        List<WkKnowledgeCatalog> wkKnowledgeCatalogs = wkKnowledgeCatalogMapper.selectJoinList(WkKnowledgeCatalog.class, lambdaWrapper);
        List<WkKnowledgeCatalog> collect = wkKnowledgeCatalogs.stream().map(item -> { item.setKnType("catalog"); return item; })
                .flatMap(item -> item.getWkKnowledgeContentList().stream().map(content -> {
                    WkKnowledgeCatalog wkKnowledgeCatalog = new WkKnowledgeCatalog();
                    wkKnowledgeCatalog.setId(content.getId());
                    wkKnowledgeCatalog.setPid(content.getCatalogId());
                    wkKnowledgeCatalog.setName(content.getTitle());
                    wkKnowledgeCatalog.setKnType("content");
                    return wkKnowledgeCatalog;
                })).collect(Collectors.toList());
         wkKnowledgeCatalogs.addAll(collect);
        return wkKnowledgeCatalogs;
    }

    @Override
    public Result addKnowledgeCatalog(WkKnowledgeCatalog catalog) {
        int insert = wkKnowledgeCatalogMapper.insert(catalog);
        if (insert > 0) {
            return Result.succ(1, "添加成功", catalog);
        } else {
            return Result.succ(400, "添加失败", "");
        }
    }

    @Override
    public Result editKnowledgeCatalog(WkKnowledgeCatalog catalog) {
        int updateById = wkKnowledgeCatalogMapper.updateById(catalog);
        if (updateById > 0) {
            return Result.succ(1, "修改成功", catalog);
        } else {
            return Result.succ(400, "修改失败", "");
        }
    }

    @Override
    public Result deletKnowledgeCatalog(WkKnowledgeCatalog catalog) {
        int deleteById = wkKnowledgeCatalogMapper.deleteById(catalog.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", catalog);
        } else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result findKnowledgeCatalogAndContentTree(KnowledgePageParam param) {
        List<Map> returnList = new ArrayList<>();

        //查出目录
        LambdaQueryWrapper<WkKnowledgeCatalog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkKnowledgeCatalog::getKnowledgeId, param.getKnowledgeId())
                .eq(WkKnowledgeCatalog::getDelFlag, 0).orderByAsc(WkKnowledgeCatalog::getSeq);
        List<WkKnowledgeCatalog> wkKnowledgeCatalogs = wkKnowledgeCatalogMapper.selectList(lambdaQueryWrapper);
        //查出目录下得内容
        List<String> cIds = wkKnowledgeCatalogs.stream().map(WkKnowledgeCatalog::getId).collect(Collectors.toList());
        if (ToolsUtils.isNotEmpty(cIds)) {
            LambdaQueryWrapper<WkKnowledgeContent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WkKnowledgeContent::getDelFlag, 0)
                    .in(WkKnowledgeContent::getCatalogId, cIds)
                    .orderByAsc(WkKnowledgeContent::getSeq);
            List<WkKnowledgeContent> wkKnowledgeContents = wkKnowledgeCatalogContentMapper.selectList(queryWrapper);
            for (WkKnowledgeCatalog catalog : wkKnowledgeCatalogs) {
                Map<String, Object> map = new HashMap();
                map.put("id", catalog.getId());
                map.put("knowledgeId", catalog.getKnowledgeId());
                map.put("pid", catalog.getPid());
                map.put("name", catalog.getName());
                map.put("code", catalog.getCode());
                map.put("seq", catalog.getSeq());
                map.put("orgId", catalog.getOrgId());
                map.put("status", catalog.getStatus());
                map.put("knType", "catalog");
                returnList.add(map);
            }

            for (WkKnowledgeContent content : wkKnowledgeContents) {
                Map<String, Object> map = new HashMap();
                map.put("id", content.getId());
                map.put("knowledgeId", content.getKnowledgeId());
                map.put("pid", content.getCatalogId());
                map.put("catalogId", content.getCatalogId());
                map.put("orgId", content.getOrgId());
                map.put("name", content.getTitle());
                map.put("title", content.getTitle());
                //map.put("content",content.getContent());
                map.put("seq", content.getSeq());
                map.put("status", content.getStatus());
                map.put("publishTime", content.getPublishTime());
                map.put("knType", "content");
                returnList.add(map);
            }

        }
        JSONArray objects = ListToTreeUtil.forObjectAndLevelToTree(returnList, "0", "id", "pid", "children", 1);
        return Result.succ(1, "查询成功", objects);
    }

}
